var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lings:'ok',
    gzs:{},
    gzflag:'',
    setting: {},
    banrdName: "",
    banrdimg: "",
    getSessionKeyUrl: "/api/member/getSessionKey.do",
    getUrl: "/api/memberCoupon/getGZCoupons.do",
    phoneModle: false,
    authModle: false,
    userInfo: {},
    ok: false,
    action: "",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    console.log(options.pmarms)
    this.setData({
      gzflag: options.pmarms
    })
    wx.showLoading({
      title: '加载中.....',
      mask: true
    })
    var parmas = {
      gzflag: options.pmarms
    }
    ux.loginGz(parmas, function (protocol) {
      self.setData({
        gzs: protocol.data.gzs
      })
      if (protocol.data.type == 1) {//已授权
        if (protocol.data.userInfo.mobile) {
          self.setData({
            ok: true,
          })
          wx.hideLoading();
        } else {
          wx.hideLoading();
          self.getSessionKey();
          self.setData({
            phoneModle: true,
          })
        }
      } else if (protocol.data.type == 2) {//未授权
        wx.hideLoading();
        self.setData({
          authModle: true,
        })
      }
    })
  },

  goToLing:function(){
    if (this.data.lings!="ok"){
      return false;
    }
    var self=this;
    var pass={
      flag:this.data.gzflag
    }
    wx.showLoading({
      title: '领取中...',
    })
    ux.request(this.data.getUrl, pass, function (protocol) {
      wx.hideLoading();
      self.setData({
        lings:"no",
        msg: protocol.data.msg
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  goToBAND: function () {
    wx.reLaunch({
      url: '/pages/member/tab1/sugarSpice?id=' + this.data.gzs.merchant,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getUserInfoFun: function (e) {
    var self = this;
    var userInfo = e.detail.userInfo
    var params = {};
    params.nickname = userInfo.nickName;
    params.header = userInfo.avatarUrl;
    params.sex = userInfo.gender;
    console.log(JSON.stringify(userInfo));
    wx.showLoading({
      mask: true,
      title: '处理中....',
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          params.jscode = res.code;
          ux.request("/api/member/registerUser.do", params,
            function (protocol) {
              if (protocol.success) {
                wx.setStorageSync("userInfo", protocol.data.userInfo);
                wx.setStorageSync("token", protocol.data.token);
                wx.setStorageSync("setting", protocol.data.setting);
                wx.hideLoading();
                self.getSessionKey();
                self.setData({
                  phoneModle: true,
                  authModle: false,
                })
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '系统繁忙!',
                })
              }
            }, true);
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
      complete: function (res) {
        console.log(JSON.stringify(res))
      }
    })
  },

  getSessionKey() {
    var self = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var parmas = { jscode: res.code }
          ux.request(self.data.getSessionKeyUrl, parmas, function (data) {
            self.setData({
              openid: data.data.openid,
              session_key: data.data.session_key
            })
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
      complete: function (res) {
        console.log(JSON.stringify(res))
      }
    })
  },
  getPhoneNumbers: function (e) {
    var self = this;
    var vi = e.detail.iv;
    var encData = e.detail.encryptedData;
    if (!vi) {
      wx.showToast({
        title: '请授权手机号码',
      })
      return false;
    }
    wx.showLoading({
      mask: true,
      title: '处理中....',
    })

    var params = { session_key: self.data.session_key, openid: self.data.openid, vi: vi, encData: encData };
    ux.request("/api/member/authPhones.do", params, function (data) {
      wx.hideLoading();
      wx.setStorageSync("userInfo", data.data.userInfo);
      wx.setStorageSync("token", data.data.token);
      wx.setStorageSync("setting", data.data.setting);
      self.toast("授权成功");
      self.setData({
        phoneModle: false,
        userInfo: data.data.userInfo,
        ok: true,
      })
    }, true);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }
})
